//
//  BaseTabBarViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/16.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "DestinationViewController.h"
#import "NearbyViewController.h"
#import "RecommendViewController.h"
#import "SettingViewController.h"
#import "BaseNavigationController.h"
@interface BaseTabBarViewController ()

@end

@implementation BaseTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    RecommendViewController *recommendViewController = [[RecommendViewController alloc] init];
    BaseNavigationController *naRecommend = [[BaseNavigationController alloc] initWithRootViewController:recommendViewController];
    naRecommend.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"推荐" image:[UIImage imageNamed:@"index.png"] tag:10001];
    
    
    NearbyViewController *nearbyViewController = [[NearbyViewController alloc] init];
    BaseNavigationController *naNearby= [[BaseNavigationController alloc] initWithRootViewController:nearbyViewController];
    naNearby.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"附近" image:[UIImage imageNamed:@"nearby.png"] tag:10002];
    
    
    DestinationViewController *destinationViewController = [[DestinationViewController alloc] init];
    BaseNavigationController *naDestination = [[BaseNavigationController alloc] initWithRootViewController:destinationViewController];
    naDestination.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"目的地" image:[UIImage imageNamed:@"ground.png"] tag:10003];


    
    SettingViewController *settingVC = [[SettingViewController alloc] init];
    settingVC.myDelegate = self;
    
    
    BaseNavigationController *naSetting = [[BaseNavigationController alloc] initWithRootViewController:settingVC];
    naSetting.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"设置" image:[UIImage imageNamed:@"setting.png"] tag:10004];

    

    self.tabBar.translucent = NO;
    self.viewControllers = [NSArray arrayWithObjects:naRecommend, naNearby, naDestination, naSetting, nil];
    

    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    if ([@"on" isEqualToString:[userDefaults objectForKey:@"isNight"]]) {
        
        //    夜间模式
        self.tabBar.barTintColor=[UIColor colorWithRed:27 / 255.0 green:27 / 255.0 blue:27 / 255.0 alpha:1.0];
        NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
        textAttr[NSForegroundColorAttributeName] = [UIColor whiteColor];
        
        NSMutableDictionary *selectTextAttr = [NSMutableDictionary dictionary];
        selectTextAttr[NSForegroundColorAttributeName] = [UIColor blueColor];
        [self.tabBarItem setTitleTextAttributes:textAttr forState:UIControlStateNormal];
        [self.tabBarItem setTitleTextAttributes:selectTextAttr forState:UIControlStateSelected];
        self.tabBar.tintColor = [UIColor whiteColor];
        
    } else {
        
        self.tabBar.barTintColor=[UIColor colorWithRed:0 / 255.0 green:186 / 255.0 blue:170 / 255.0 alpha:1.0];
        NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
        textAttr[NSForegroundColorAttributeName] = [UIColor whiteColor];
        
        NSMutableDictionary *selectTextAttr = [NSMutableDictionary dictionary];
        selectTextAttr[NSForegroundColorAttributeName] = [UIColor blueColor];
        [self.tabBarItem setTitleTextAttributes:textAttr forState:UIControlStateNormal];
        [self.tabBarItem setTitleTextAttributes:selectTextAttr forState:UIControlStateSelected];
        self.tabBar.tintColor = [UIColor blueColor];
        
    }
    
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(changeNight:) name:@"changeNight" object:nil];

    
}
-(void)nightChange {
    [self.myDelegate nightChange];
    NSLog(@"2222");
    
}

-(void)changeNight:(NSNotification *)notification {
    

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([@"on" isEqualToString:[userDefaults objectForKey:@"isNight"]]) {

        self.tabBar.barTintColor=[UIColor colorWithRed:27 / 255.0 green:27 / 255.0 blue:27 / 255.0 alpha:1.0];
        NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
        textAttr[NSForegroundColorAttributeName] = [UIColor whiteColor];
        
        NSMutableDictionary *selectTextAttr = [NSMutableDictionary dictionary];
        selectTextAttr[NSForegroundColorAttributeName] = [UIColor blueColor];
        [self.tabBarItem setTitleTextAttributes:textAttr forState:UIControlStateNormal];
        [self.tabBarItem setTitleTextAttributes:selectTextAttr forState:UIControlStateSelected];
        self.tabBar.tintColor = [UIColor whiteColor];
    } else {

        self.tabBar.barTintColor=[UIColor colorWithRed:0 / 255.0 green:186 / 255.0 blue:170 / 255.0 alpha:1.0];
        NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
        textAttr[NSForegroundColorAttributeName] = [UIColor whiteColor];
        
        NSMutableDictionary *selectTextAttr = [NSMutableDictionary dictionary];
        selectTextAttr[NSForegroundColorAttributeName] = [UIColor blueColor];
        [self.tabBarItem setTitleTextAttributes:textAttr forState:UIControlStateNormal];
        [self.tabBarItem setTitleTextAttributes:selectTextAttr forState:UIControlStateSelected];
        self.tabBar.tintColor = [UIColor blueColor];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
